
package card;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author DA5221-GRP63
 */

public class loginFrame extends JFrame{
  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();
  String CardID;
  String loginQuery;
  String passwordtemp;
  PreparedStatement loginStmt;
  Connection con;
  ResultSet rs;
  JLabel jLabel1 = new JLabel();
  JPanel jPanel1 = (JPanel)getGlassPane();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JTextField jTextField1 = new JTextField();
  JPasswordField jTextField2 = new JPasswordField();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  ImageIcon img1;

  public loginFrame()
  {
    try
    {
      setDefaultCloseOperation( EXIT_ON_CLOSE );
      setUndecorated(true);
      jPanel1.addMouseMotionListener(new moveFrameEvent(this));
      setResizable(false);
      jbInit();
    } catch( Exception exception )
    {
      exception.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    contentPane = ( JPanel ) getContentPane();
    contentPane.setLayout( borderLayout1 );
    setSize( new Dimension( 385, 320 ) );
    setTitle( "The Management System Of Credit Card" );
    img1=new ImageIcon("img/1.jpg");
    jLabel1.setIcon( img1 );
    jLabel1.setText( "" );
    jPanel1.setLayout( null );
    jLabel3.setFont( new java.awt.Font( "Times New Roman", Font.BOLD, 18 ) );
    jLabel3.setText( "ID:" );
    jLabel3.setBounds( new Rectangle( 64, 103, 48, 22 ) );
    jLabel4.setFont( new java.awt.Font( "Times New Roman", Font.BOLD, 18 ) );
    jLabel4.setText( "PIN:" );
    jLabel4.setBounds( new Rectangle( 66, 163, 53, 24 ) );
    jTextField1.setBounds( new Rectangle( 138, 102, 155, 23 ) );
    jTextField1.setBorder( BorderFactory.createLineBorder( Color.black ) );
    jTextField1.setBackground(new Color(84,103,206));
    jTextField1.addActionListener( new ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        jTextField1_actionPerformed( e );
      }
    } );
    jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusLost(FocusEvent e) {
        text1_focusLost(e);
      }
    });
    jTextField1.addFocusListener(new java.awt.event.FocusAdapter()
    {
      public void focusGained(FocusEvent e)
      {
        text1_focusGained(e);
      }
    });

    jTextField2.setBounds( new Rectangle( 138, 161, 155, 22 ) );
    jTextField2.setBorder( BorderFactory.createLineBorder( Color.black ) );
    jTextField2.setBackground(new Color(84,103,206));
    jTextField2.addActionListener( new ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        jTextField2_actionPerformed( e );
      }
    } );
    jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusLost(FocusEvent e) {
        text1_focusLost(e);
      }
    });
    jTextField2.addFocusListener(new java.awt.event.FocusAdapter()
    {
      public void focusGained(FocusEvent e)
      {
        text1_focusGained(e);
      }
    });

    jButton1.setBounds( new Rectangle( 72, 219, 84, 20 ) );
    jButton1.setFont( new java.awt.Font( "Times New Roman", Font.BOLD, 12 ) );
    jButton1.setText( "Login" );
    jButton1.setBackground(new Color(84,103,206));
    jButton1.addActionListener( new ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        jButton1_actionPerformed( e );
      }
    } );
    
    jButton2.setBounds( new Rectangle( 201, 217, 90, 22 ) );
    jButton2.setFont( new java.awt.Font( "Times New Roman", Font.BOLD, 12 ) );
    jButton2.setText( "Quit" );
    jButton2.setBackground(new Color(84,103,206));
    jButton2.addActionListener( new ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        jButton2_actionPerformed( e );
      }
    } );

    jPanel1.add( jTextField2 );
    jPanel1.add( jLabel3 );
    jPanel1.add( jButton2 );
    jPanel1.add( jTextField1 );
    jPanel1.add( jButton1 );
    jPanel1.add( jLabel4 );
    jPanel1.setVisible(true);
    contentPane.add( jLabel1, java.awt.BorderLayout.CENTER );

    Connection con=cardConnect.getconn();
    loginQuery="SELECT * FROM card Where CardID=? and Password=?";
    loginStmt=con.prepareStatement(loginQuery);
  }

  void text1_focusLost(FocusEvent e)
  {
    JTextField jt=(JTextField)e.getSource();
    jt.setBackground(new Color(84,103,206));
  }
  void text1_focusGained(FocusEvent e)
  {
    JTextField jt=(JTextField)e.getSource();
    jt.setBackground(Color.white);
  }

  public void jButton1_actionPerformed( ActionEvent e )
  {
    protocol();
  }

  public void jButton2_actionPerformed( ActionEvent e )
  {
    System.exit(0);
  }

  //Warn
  public void showMessageDialog(String s1,String s2)
  {
    JOptionPane.showMessageDialog(this,s1,s2,JOptionPane.ERROR_MESSAGE);
  }
  //Login
  public void protocol()
  {
    if(jTextField1.getText().trim().equals(""))
    {
      showMessageDialog("Please Input UserName！","Warning");
      jTextField1.selectAll();
      jTextField1.requestFocus();
      return;
    }
    if(jTextField2.getPassword().toString().equals(""))
    {
      showMessageDialog("Please Input Password！","Warning");
      jTextField2.setText("");
      jTextField2.requestFocus();
      return;
    }
    CardID=jTextField1.getText();//get the value of CardID
    try
    {
      loginStmt.setString( 1, CardID ); 
      passwordtemp="";
      for(int i=0;i<jTextField2.getPassword().length;i++)
      {
        passwordtemp+=jTextField2.getPassword()[i];
      }//get the value of password
      loginStmt.setString(2,passwordtemp);
      rs=loginStmt.executeQuery();
      if(rs.next())//check the cardID and password
      {
        eventFrame ef=new eventFrame();//pass the checking
        ef.setCardID(CardID);
        ef.setVisible(true);
        this.setVisible(false);
        this.dispose();
      }else//doesn't pass the checking
      {
        showMessageDialog("UserName Or Password Is Wrong！","Warning");
        jTextField1.selectAll();
        jTextField1.requestFocus();
        jTextField2.setText("");
      }
    }catch(SQLException ex)
    {
      System.out.println("Datebase Wrong！");
      return;
    }
  }

  public void jTextField1_actionPerformed( ActionEvent e )
  {
    jTextField2.requestFocus();
  }

  public void jTextField2_actionPerformed( ActionEvent e )
  {
    protocol();
  }

}
